package com.bonc.ioc.controller;

import com.bonc.ioc.base.controller.BaseController;
import com.bonc.ioc.base.page.PageResult;
import com.bonc.ioc.common.util.AppReply;
import com.bonc.ioc.service.ScientificService;
import com.bonc.ioc.util.DictionaryEnum;
import com.bonc.ioc.vo.PageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @author liuyang
 * @creats 2021-04-08-19:02
 */
@RestController
@RequestMapping("/scientific")
@Slf4j
public class ScientificController extends BaseController {
    @Autowired
    private ScientificService scientificService;

    /**
     * getStandardInfo
     * 获取知识产权 详情
     *
     * @param id:
     * @return com.bonc.ioc.common.util.AppReply
     */
    @GetMapping("/getScientificInfo")
    // @AppAuthAnnotation
    public AppReply getStandardInfo(@RequestParam String id) {
        AppReply success = AppReply.success(scientificService.selectByScientificId(Long.valueOf(id)));
        if (success.getCode().equals(AppReply.SUCCESS_CODE)) {
            scientificService.updateVisitNum(Long.valueOf(id));
        }
        return success;
    }

    /**
     * 科研软件--分页--检索
     *
     * @return com.bonc.ioc.common.util.AppRepl
     * @throws BindException
     */
    @GetMapping("/getScientificByPage")
    public AppReply getStandardInfoByPage(PageVo page) throws BindException {
        HashMap<String, String> map = new HashMap<>(16);
        map.put("pageSize", page.getPageSize().trim());
        map.put("pageNum", page.getPageNum().trim());
        map.put("title", page.getTitle().trim());
        map.put("keys", page.getKeys().trim());
        map.put("desc", page.getDesc().trim());
        map.put("type", page.getType().trim());
        map.put("orderBy", page.getOrderBy().trim());
        PageResult pageResult = scientificService.selectStandardInfoByPage(map);
        return AppReply.success(pageResult);
    }

    /**
     * 更新下载量
     *
     * @param id 主键id
     * @return com.bonc.ioc.common.util.AppReply
     */
    @GetMapping("/Downloads")
    // @AppAuthAnnotation
    public AppReply updateDownloads(@RequestParam String id) {
        return AppReply.success(scientificService.updateDownloadsNum(Long.valueOf(id)));
    }

    /**
     * 根据关键词查询title
     *
     * @param keys keys
     * @return com.bonc.ioc.common.util.AppReply
     */
    @GetMapping("/selectBykeys")
    //@AppAuthAnnotation
    public AppReply selectBykeys(@RequestParam String keys,
                                 @RequestParam Integer id,
                                 @RequestParam String title) {
        return AppReply.success(scientificService.selectTitleBykeys(id, keys, title));
    }

    /**
     * 根据创建时间排序查询title
     *
     * @return com.bonc.ioc.common.util.AppReply
     */
    @GetMapping("/selectByCreateTime")
    //  @AppAuthAnnotation
    public AppReply selectByCreateTime(@RequestParam(defaultValue = "5") String pageSize, @RequestParam(defaultValue = "0") String pageNum) {
        return AppReply.success(scientificService.selectByCreateTime(pageSize, pageNum));
    }

    /**
     * 查询分类
     *
     * @return com.bonc.ioc.common.util.AppReply
     */
    @GetMapping("/getScientificDirs")
    public AppReply getScientificDirs(String type) {
        DictionaryEnum dictionaryEnum = DictionaryEnum.valueOf(type.toUpperCase());
        return AppReply.success(scientificService.getScientificDirs(dictionaryEnum.getValue()));
    }
}
